package daily.play2;

//同步代码块
/*class MyThread extends Thread{
    static int ticket = 1000;
    public MyThread(String name){
        super(name);
    }
    public void run(){
        while(true){
            synchronized (MyThread.class){
                if(ticket>0){
                    try {
                        Thread.sleep(30);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket--;
                    System.out.println(getName()+"卖了1张票，还有"+ticket+"张");
                }else {
                    break;
                }
            }
        }
    }
}
public class demo1 {
    public static void main(String[] args) {
        MyThread thread1  = new MyThread("窗口1");
        MyThread thread2  = new MyThread("窗口2");
        thread1.start();
        thread2.start();
    }
}*/

//同步方法
/*class MyThread extends Thread {
    static int ticket = 1000;

    public MyThread(String name) {
        super(name);
    }

    public void run() {
        while (true) {
            if(!payTicket())
                break;
        }
    }

    public synchronized boolean payTicket() {
        if (ticket > 0) {
            try {
                Thread.sleep(30);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            ticket--;
            System.out.println(getName() + "卖了1张票，还有" + ticket + "张");
        } else {
            return false;
        }
        return true;
    }
}
public class demo1 {
    public static void main(String[] args) {
        MyThread thread1  = new MyThread("窗口1");
        MyThread thread2  = new MyThread("窗口2");
        thread1.start();
        thread2.start();
    }
}*/

/*import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class MyThread extends Thread{
    static Lock lock = new ReentrantLock();
    static int ticket = 1000;
    public MyThread(String name){
        super(name);
    }
    public void run(){
        while(true){
                    try {
                        lock.lock();
                        if(ticket>0){
                            Thread.sleep(30);
                            ticket--;
                            System.out.println(getName()+"卖了1张票，还有"+ticket+"张");
                        }else {
                            break;
                        }
                    }catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }finally {
                        lock.unlock();
                    }
                }
    }
}
public class demo1 {
    public static void main(String[] args) {
        MyThread thread1  = new MyThread("窗口1");
        MyThread thread2  = new MyThread("窗口2");
        thread1.start();
        thread2.start();
    }
}*/

/*class MyThread extends Thread{
    static int sum = 100;
    public void run(){
        while (true){
            synchronized (MyThread.class){
                if(sum<10){
                    break;
                }else{
                    sum--;
                    try {
                        Thread.sleep(20);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(getName()+"送出了一份礼物，还剩"+sum+"份礼物");
                }
            }
        }
    }
    public MyThread(String name){
        super(name);
    }
}

public class demo1{
    public static void main(String[] args) {
        MyThread thread1  = new MyThread("员工1");
        MyThread thread2  = new MyThread("员工2");
        thread1.start();
        thread2.start();
    }
}*/

/*class MyThread extends Thread{
    static int number = 1;
    public MyThread(String name){
        super(name);
    }
    public void run(){
        while(true){
            synchronized (MyThread.class){
                if(number<101){
                    if(number%2==1){
                        System.out.println(getName()+"查出了数字"+number);
                    }
                    number++;
                }else{
                    break;
                }
            }
        }
    }
}

public class demo1{
    public static void main(String[] args) {
        MyThread thread1 = new MyThread("thread1");
        MyThread thread2 = new MyThread("thread2");
        thread1.start();
        thread2.start();
    }
}*/

/*import java.util.Random;

class MyThread extends Thread{
    static int num = 3;
    Random ran = new Random();
    static int money = 100;
    public MyThread(String name){
        super(name);
    }
    public void run(){
        synchronized (MyThread.class){
            while(true){
                if(money==0||num==0){
                    System.out.println(getName()+"没有抢到");
                    break;
                }
                int timeMoney = ran.nextInt(1,money+2-num);
                if(num==1){
                    System.out.println(getName()+"抢到了"+money+"元");
                    num--;
                    break;
                }
                if(timeMoney <= money){
                    money-=timeMoney;
                    System.out.println(getName()+"抢到了"+timeMoney+"元");
                    num--;
                    break;
                }
            }
        }
    }
}

public class demo1{
    public static void main(String[] args){
        MyThread thread1 = new MyThread("1号");
        MyThread thread2 = new MyThread("2号");
        MyThread thread3 = new MyThread("3号");
        MyThread thread4 = new MyThread("4号");
        MyThread thread5 = new MyThread("5号");
        thread1.start();
        thread2.start();
        thread3.start();
        thread4.start();
        thread5.start();
    }
}*/

/*import com.sun.jdi.ArrayReference;

import javax.naming.Name;
import java.sql.SQLOutput;
import java.util.ArrayList;
import java.util.Collections;

//奖池抽奖
class MyThread extends Thread {
    ArrayList<Integer> list = new ArrayList<>();
    public MyThread(ArrayList<Integer> list,String Name){
        super(Name);
        this.list = list;
    }
    public void run(){
        ArrayList<Integer> listtemp = new ArrayList<>();
        while(true){
            synchronized (MyThread.class){
                if(list.size()!=0){
                    Collections.shuffle(list);
                    int temp = list.remove(0);
                    listtemp.add(temp);
                    System.out.println(getName()+"抽到了"+temp);
                }else{
                    break;
                }
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        System.out.println(getName()+":"+listtemp);
    }
}

public class demo1{
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list,11,22,33,44,55,66,77,78);
        MyThread thread1 = new MyThread(list,"箱子1");
        thread1.start();
        MyThread thread2 = new MyThread(list,"箱子2");
        thread2.start();
    }
}*/

import javax.print.attribute.standard.PagesPerMinute;
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
import java.util.concurrent.*;

//线程池
/*class MyThread extends Thread{
    public void run(){
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName()+"---"+i);
        }
    }
}

public class demo1{
    public static void main(String[] args) {
        //创建一个线程池
        ExecutorService es = Executors.newFixedThreadPool(2);
        //提交任务
        es.submit(new MyThread());
        es.submit(new MyThread());
        es.submit(new MyThread());
        es.submit(new MyThread());
        //销毁线程池，不然程序不会停止
        es.shutdown();
    }
}*/

//复习线程池



/*public class demo1{
    public static void main(String[] args) {
        ExecutorService es = Executors.newCachedThreadPool();
        es.submit(new Task());
        es.submit(new Task());
        es.submit(new Task());
        es.shutdown();
    }
}*/

//自定义线程池
//创建一个任务
/*
class Task implements Runnable{
    public void run(){
        for (int i = 0; i < 100; i++) {
            System.out.println(Thread.currentThread().getName()+"-----"+i);
        }
    }
}
public class demo1{
    public static void main(String[] args) {
        ThreadPoolExecutor tpe = new ThreadPoolExecutor(
                3,//核心线程的数量
                3,//临时线程的数量
                60, //临时线程最大的存活时间
                TimeUnit.SECONDS,//对应时间的单位
                new ArrayBlockingQueue<>(3),//创建一个队列，用来任务排列
                Executors.defaultThreadFactory(),//创建一个线程工厂,这个工厂用来创建线程池中的线程的。
                new ThreadPoolExecutor.AbortPolicy());//任务的拒绝策略
        tpe.submit(new Task());
        tpe.submit(new Task());
        tpe.submit(new Task());
        tpe.submit(new Task());
        tpe.shutdown();
    }
}*/

/*public class demo1{
    public static void main(String[] args) {
        System.out.println(Runtime.getRuntime().availableProcessors());
    }
}*/


//网络编程
/*public class demo1{
    public static void main(String[] args) throws UnknownHostException {
        //可以通过域名或者主机名（电脑名）获取ip地址
        InetAddress address = InetAddress.getByName("www.baidu.com");
        //此时的address表示的是名称加ip
        System.out.println(address);
        //获取单独的名称
        System.out.println(address.getHostName());
        //获取单独的ip
        System.out.println(address.getHostAddress());
    }
}*/

//UDP协议通信
/*public class demo1{
    public static void main(String[] args) throws IOException {
        //创建一个通讯工具
        //无参就是随机使用一个默认的本机端口发送信息
        //有参就是指定使用某个端口发送信息
        DatagramSocket ds = new DatagramSocket();
        //打包数据
        String str = "Hello World";
        byte[] arr = str.getBytes();
        InetAddress address = InetAddress.getByName("127.0.0.1");
        int port = 1111;
        DatagramPacket dp = new DatagramPacket(arr,arr.length,address,port);
        //发送数据
        ds.send(dp);
        //释放资源
        ds.close();
    }
}*/

//UDP接收数据
/*public class demo1{
    public static void main(String[] args) throws IOException {
        //创建一个负责数据传输的工具
        //接收端一定要写负责接收的端口，而且要和发送段写的接收端口保持一致
        DatagramSocket ds = new DatagramSocket(1111);
        //创建一个包用来管理接收的数据
        byte[] arr = new byte[1024];
        DatagramPacket dp = new DatagramPacket(arr, arr.length);
        ds.receive(dp);
        System.out.println(dp.getAddress());
        System.out.println(dp.getPort());
        System.out.println(new String(dp.getData(), 0, dp.getLength()));
        //释放资源
        ds.close();
    }
}*/

//聊天室的发送端
//public class demo1{
//    public static void main(String[] args) throws IOException {
//        DatagramSocket ds = new DatagramSocket();
//        Scanner sc = new Scanner(System.in);
//        InetAddress address = InetAddress.getByName("127.0.0.1");
//        int port = 1111;
//        while(true){
//            System.out.println("请输入发送的信息：");
//            String arr = sc.nextLine();
//            if("886".equals(arr)){
//                System.out.println("聊天结束！");
//                break;
//            }
//            DatagramPacket dp = new DatagramPacket(arr.getBytes(), arr.getBytes().length, address, port);
//            ds.send(dp);
//        }
//        ds.close();
//    }
//}
